home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / gui / textfield.lha / TextField / Source / TestClass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-27  |  10.1 KB  |  342 lines

  1. /*
  2.  * TestClass.c
  3.  *
  4.  * This is a sample program that uses the TextField BOOPSI class.
  5.  * It is modeled after an example in the RKRM Libraries book on
  6.  * the RKMButClass.  It is provided to show how to use a TextField.gadget
  7.  * BOOPSI object and shows general techniques and functionality.
  8.  *
  9.  * Opening the TextField.gadget library is done by TextFieldAuto.c.
  10.  *
  11.  * This code compiles with SAS/C 6.51.
  12.  */
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #include <exec/types.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/classes.h>
  21. #include <intuition/classusr.h>
  22. #include <intuition/imageclass.h>
  23. #include <intuition/gadgetclass.h>
  24. #include <intuition/cghooks.h>
  25. #include <intuition/icclass.h>
  26. #include <graphics/gfxmacros.h>
  27. #include <graphics/text.h>
  28. #include <utility/tagitem.h>
  29. #include <utility/hooks.h>
  30. #include <gadgets/textfield.h>
  31.  
  32. #include <clib/alib_protos.h>
  33.  
  34. #include <proto/exec.h>
  35. #include <proto/iffparse.h>
  36. #include <proto/intuition.h>
  37. #include <proto/graphics.h>
  38. #include <proto/utility.h>
  39. #include <proto/textfield.h>
  40.  
  41. extern int SetupScreen( void );
  42. extern void CloseDownScreen( void );
  43. extern int HandleTextFieldPrefsIDCMP( void );
  44. extern int OpenTextFieldPrefsWindow( void );
  45. extern void CloseTextFieldPrefsWindow( void );
  46. extern struct Window        *TextFieldPrefsWnd;
  47.  
  48. static const char vers[] = "\0$VER: TestTextField 1.1 " __AMIGADATE__;
  49.  
  50. void MainLoop(void);
  51. BOOL SetupPrefsWindow(void);
  52. void ShutdownPrefsWindow(void);
  53.  
  54. Class *textfield_class;
  55. struct Window *window;
  56. struct DrawInfo *draw_info;
  57. struct Gadget *edit_object, *prop_object, *up_object, *down_object;
  58. struct Image *up_image, *down_image;
  59. struct IntuiMessage *msg;
  60. struct ClipboardHandle *clip_handle, *undo_handle;
  61. unsigned char *text_buffer, *my_buffer;
  62. ULONG length, window_sig, prefs_sig, sigs, style;
  63. UWORD *pens;
  64.  
  65. char    initial_text[] = "Sample text placed immediately into the gadget.\nType into the object.\nOr try AMIGA-[, AMIGA-=, AMIGA-], or AMIGA-\\.\n",
  66.         more_text[] = "I think the gadget looks best with the double-bevel border and a medium cursor speed.";
  67.  
  68. struct TagItem prop2edit[] = {
  69.     { PGA_Top, TEXTFIELD_Top },
  70.     { TAG_DONE }
  71. };
  72.  
  73. struct TagItem edit2prop[] = {
  74.     { TEXTFIELD_Top, PGA_Top },
  75.     { TEXTFIELD_Visible, PGA_Visible },
  76.     { TEXTFIELD_Lines, PGA_Total },
  77.     { TAG_DONE }
  78. };
  79.  
  80. struct TagItem up2edit[] = {
  81.     { GA_ID, TEXTFIELD_Up },
  82.     { TAG_DONE }
  83. };
  84.  
  85. struct TagItem down2edit[] = {
  86.     { GA_ID, TEXTFIELD_Down },
  87.     { TAG_DONE }
  88. };
  89.  
  90. void main(void)
  91. {
  92.     if (!SetupPrefsWindow()) {
  93.         return;
  94.     }
  95.     prefs_sig = 1L << TextFieldPrefsWnd->UserPort->mp_SigBit;
  96.  
  97.     /* Open the window */
  98.     window = OpenWindowTags(NULL,    WA_Flags,            WFLG_DEPTHGADGET|WFLG_DRAGBAR|WFLG_CLOSEGADGET|WFLG_SIZEGADGET
  99.                                                         |WFLG_SIZEBBOTTOM|WFLG_SIZEBRIGHT,
  100.                                     WA_Activate,        TRUE,
  101.                                     WA_IDCMP,            IDCMP_CLOSEWINDOW,
  102.                                     WA_Width,            320,
  103.                                     WA_Height,            200,
  104.                                     WA_NoCareRefresh,    TRUE,
  105.                                     WA_ScreenTitle,        "Testing textfield.gadget",
  106.                                     TAG_END);
  107.  
  108.     if (window) {
  109.         window_sig = 1L << window->UserPort->mp_SigBit;
  110.         sigs = prefs_sig | window_sig;
  111.         /* Get the DrawInfo since sysiclass needs it */
  112.         draw_info = GetScreenDrawInfo(window->WScreen);
  113.  
  114.         if (draw_info) {
  115.             pens = draw_info->dri_Pens;
  116.  
  117.             /* Make some changes in the window limits */
  118.             WindowLimits(window, 160, window->BorderTop + window->BorderBottom + window->WScreen->RastPort.TxHeight + 46, -1, -1);
  119.  
  120.             /* Get the handle on the TextField class */
  121.             textfield_class = TEXTFIELD_GetClass();
  122.  
  123.             /* Create the gadgets/images with interconnection */
  124.             prop_object = NewObject(NULL, "propgclass",
  125.                                     GA_ID,            2,
  126.                                     GA_Top,            window->BorderTop,
  127.                                     GA_RelRight,    -(window->BorderRight - 5),
  128.                                     GA_Width,        window->BorderRight - 8,
  129.                                     GA_RelHeight,    -(window->BorderTop + (3 * window->BorderBottom)) - 2,
  130.                                     GA_RightBorder,    TRUE,
  131.                                     ICA_MAP,        prop2edit,
  132.                                     PGA_NewLook,    TRUE,
  133.                                     PGA_Visible,    50,        /* will get set later */
  134.                                     PGA_Total,        50,        /* will get set later */
  135.                                     TAG_END);
  136.  
  137.             up_image = NewObject(NULL, "sysiclass",
  138.                                     SYSIA_DrawInfo,    draw_info,
  139.                                     SYSIA_Which,    UPIMAGE,
  140.                                     IA_Width,        window->BorderRight,
  141.                                     IA_Height,        window->BorderBottom,
  142.                                     TAG_END);
  143.  
  144.             down_image = NewObject(NULL, "sysiclass",
  145.                                     SYSIA_DrawInfo,    draw_info,
  146.                                     SYSIA_Which,    DOWNIMAGE,
  147.                                     IA_Width,        window->BorderRight,
  148.                                     IA_Height,        window->BorderBottom,
  149.                                     TAG_END);
  150.  
  151.             up_object = NewObject(NULL, "buttongclass",
  152.                                     GA_RelBottom,    -(3 * window->BorderBottom) - 1,
  153.                                     GA_RelRight,    -(window->BorderRight - 1),
  154.                                     GA_Height,        window->BorderBottom,
  155.                                     GA_Width,        window->BorderRight,
  156.                                     GA_Image,        up_image,
  157.                                     GA_RightBorder,    TRUE,
  158.                                     GA_RelVerify,    TRUE,
  159.                                     GA_Previous,    prop_object,
  160.                                     ICA_MAP,        up2edit,
  161.                                     TAG_END);
  162.  
  163.             down_object = NewObject(NULL, "buttongclass",
  164.                                     GA_RelBottom,    -(2 * window->BorderBottom),
  165.                                     GA_RelRight,    -(window->BorderRight - 1),
  166.                                     GA_Height,        window->BorderBottom,
  167.                                     GA_Width,        window->BorderRight,
  168.                                     GA_Image,        down_image,
  169.                                     GA_RightBorder,    TRUE,
  170.                                     GA_RelVerify,    TRUE,
  171.                                     GA_Previous,    up_object,
  172.                                     ICA_MAP,        down2edit,
  173.                                     TAG_END);
  174.  
  175.             /* Open the clipboard; no need to verify */
  176.             clip_handle = OpenClipboard(0);
  177.             undo_handle = OpenClipboard(42);
  178.  
  179.             edit_object = NewObject(textfield_class, NULL,
  180.                                     GA_ID,                1,
  181.                                     GA_Top,                window->BorderTop + 20,
  182.                                     GA_Left,            window->BorderLeft + 20,
  183.                                     GA_RelWidth,        -(window->BorderLeft + window->BorderRight + 40),
  184.                                     GA_RelHeight,        -(window->BorderTop + window->BorderBottom + 40),
  185.                                     GA_Previous,        down_object,
  186.  
  187.                                     ICA_MAP,            edit2prop,
  188.                                     ICA_TARGET,            prop_object,
  189.  
  190.                                     TEXTFIELD_Text,            (ULONG)initial_text,
  191.                                     TEXTFIELD_UserAlign,        TRUE,
  192.                                     TEXTFIELD_ClipStream,    clip_handle,
  193.                                     TEXTFIELD_ClipStream2,    undo_handle,
  194.                                     TEXTFIELD_CursorPos,        10,
  195.  
  196.                                     /*TEXTFIELD_VCenter,        TRUE,
  197.                                     TEXTFIELD_BlinkRate,        500000,
  198.                                     TEXTFIELD_Border,        TEXTFIELD_BORDER_DOUBLEBEVEL,
  199.                                     TEXTFIELD_Partial,        TRUE,
  200.                                     TEXTFIELD_MaxSize,        1000,
  201.                                     TEXTFIELD_Alignment,        TEXTFIELD_ALIGN_CENTER,
  202.                                     TEXTFIELD_TextAttr,        (ULONG)&font,
  203.                                     TEXTFIELD_Spacing,        10,
  204.                                     TEXTFIELD_FontStyle,        style,*/
  205.                                     TAG_END);
  206.  
  207.             /* Check if they were all created okay */
  208.             if (edit_object && prop_object && up_image && down_image && up_object && down_object) {
  209.                 ULONG cur;
  210.  
  211.                 /* Adjust some of the interconnection problems */
  212.                 SetGadgetAttrs(prop_object, window, NULL, ICA_TARGET, edit_object, TAG_END);
  213.                 SetGadgetAttrs(up_object, window, NULL, ICA_TARGET, edit_object, TAG_END);
  214.                 SetGadgetAttrs(down_object, window, NULL, ICA_TARGET, edit_object, TAG_END);
  215.  
  216.                 AddGList(window, prop_object, -1, -1, NULL);
  217.                 RefreshGList(prop_object, window, NULL, -1);
  218.  
  219.                 ActivateGadget(edit_object, window, NULL);
  220.                 SetWindowTitles(window, "<-- Click to remember cursor position (starts at pos 10)", (UBYTE *)-1);
  221.                 MainLoop();
  222.                 GetAttr(TEXTFIELD_CursorPos, edit_object, &cur);
  223.  
  224.                 SetWindowTitles(window, "<-- Click to reset cursor position", (UBYTE *)-1);
  225.                 MainLoop();
  226.                 SetGadgetAttrs(edit_object, window, NULL, TEXTFIELD_CursorPos, cur, TAG_DONE);
  227.                 ActivateGadget(edit_object, window, NULL);
  228.  
  229.                 SetWindowTitles(window, "<-- Click to replace text", (UBYTE *)-1);
  230.                 MainLoop();
  231.                 SetGadgetAttrs(edit_object, window, NULL, TEXTFIELD_Text, more_text, TAG_DONE);
  232.                 ActivateGadget(edit_object, window, NULL);
  233.  
  234.                 SetWindowTitles(window, "<-- Click to clear text", (UBYTE *)-1);
  235.                 MainLoop();
  236.                 SetGadgetAttrs(edit_object, window, NULL, TEXTFIELD_Text, "", TAG_DONE);
  237.                 ActivateGadget(edit_object, window, NULL);
  238.  
  239.                 SetWindowTitles(window, "<-- Click to print text and info", (UBYTE *)-1);
  240.                 MainLoop();
  241.                 SetGadgetAttrs(edit_object, window, NULL, GA_Disabled, TRUE, TEXTFIELD_NoGhost, TRUE, TAG_DONE);
  242.                 if (GetAttr(TEXTFIELD_Size, edit_object, &length)) {
  243.                     my_buffer = malloc(length + 1);
  244.                     if (my_buffer) {
  245.                         my_buffer[length] = 0;
  246.                         if (GetAttr(TEXTFIELD_Text, edit_object, (ULONG *)&text_buffer)) {
  247.                             if (text_buffer) {
  248.                                 memcpy(my_buffer, text_buffer, length);
  249.                                 printf("%s", my_buffer);
  250.                                 printf("\n");
  251.                             }
  252.                         }
  253.                         free(my_buffer);
  254.                     }
  255.                 }
  256.                 SetGadgetAttrs(edit_object, window, NULL, GA_Disabled, FALSE, TEXTFIELD_NoGhost, FALSE, TAG_DONE);
  257.                 if (GetAttr(TEXTFIELD_Visible, edit_object, &length)) {
  258.                     printf("Visible lines: %d\n", length);
  259.                 }
  260.                 if (GetAttr(TEXTFIELD_Lines, edit_object, &length)) {
  261.                     printf("  Total lines: %d\n", length);
  262.                 }
  263.                 if (GetAttr(TEXTFIELD_Size, edit_object, &length)) {
  264.                     printf("         Size: %d\n", length);
  265.                 }
  266.                 if (GetAttr(TEXTFIELD_Top, edit_object, &length)) {
  267.                     printf("     Top line: %d\n", length);
  268.                 }
  269.  
  270.                 SetWindowTitles(window, "<-- Click to quit", (UBYTE *)-1);
  271.                 MainLoop();
  272.  
  273.                 RemoveGList(window, prop_object, -1);
  274.             } else {
  275.                 printf("Couldn't get objects.\n");
  276.             }
  277.  
  278.             /* Clean up any objects */
  279.             DisposeObject(edit_object);
  280.             DisposeObject(down_object);
  281.             DisposeObject(up_object);
  282.             DisposeObject(down_image);
  283.             DisposeObject(up_image);
  284.             DisposeObject(prop_object);
  285.  
  286.             /* Close the clipboard */
  287.             if (undo_handle) {
  288.                 CloseClipboard(undo_handle);
  289.             }
  290.             if (clip_handle) {
  291.                 CloseClipboard(clip_handle);
  292.             }
  293.  
  294.             FreeScreenDrawInfo(window->WScreen, draw_info);
  295.         } else {
  296.             printf("Couldn't get draw info.\n");
  297.         }
  298.         CloseWindow(window);
  299.     } else {
  300.         printf("Couldn't open window.\n");
  301.     }
  302.  
  303.     ShutdownPrefsWindow();
  304. }
  305.  
  306. void MainLoop(void)
  307. {
  308.     ULONG done = FALSE;
  309.  
  310.     while (!done) {
  311.         HandleTextFieldPrefsIDCMP();
  312.         while (msg = (struct IntuiMessage *)GetMsg((struct MsgPort *)window->UserPort)) {
  313.             if (msg->Class == IDCMP_CLOSEWINDOW) {
  314.                 done = TRUE;
  315.             }
  316.             ReplyMsg((struct Message *)msg);
  317.         }
  318.         if (!done) {
  319.             Wait(sigs);
  320.         }
  321.     }
  322. }
  323.  
  324. BOOL SetupPrefsWindow(void)
  325. {
  326.     if (SetupScreen() == 0) {
  327.         if (OpenTextFieldPrefsWindow() == 0) {
  328.             return TRUE;
  329.         } else {
  330.             CloseDownScreen();
  331.         }
  332.     }
  333.  
  334.     return FALSE;
  335. }
  336.  
  337. void ShutdownPrefsWindow(void)
  338. {
  339.     CloseTextFieldPrefsWindow();
  340.     CloseDownScreen();
  341. }
  342.